package com.hjb.uils;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author: COS
 * @time: 2021/8/12 15:27
 * @description:
 */
public class DateUtils {
    private final static SimpleDateFormat[] sdf = new SimpleDateFormat[5];

    static {
        sdf[0] = new SimpleDateFormat("yyyy-MM-dd");
        sdf[1] = new SimpleDateFormat("yyyy/MM/dd");
        sdf[2] = new SimpleDateFormat("MM/dd/yyyy");
        sdf[3] = new SimpleDateFormat("MM/dd/yyyy");
        sdf[4] = new SimpleDateFormat("yyyy年MM月dd日");
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/8/12 15:33
     * @description: 日期转字符串
     */
    public static String dateToString(java.util.Date date){
        if (date == null) {
            return "用户太懒没有填写";
        }
        return sdf[0].format(date);
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/8/12 15:31
     * @description: 字符串转日期
     */
    public static java.util.Date stringToDate(String date) {
        for (int i = 0; i < sdf.length; i++) {
            try {
                return sdf[i].parse(date);
            } catch (ParseException e) {}
        }
        return null;
    }


    /**
     * @author: COS
     * @return:
     * @time: 2021/8/12 15:28
     * @description: 把util的date转为 sql的Date
     */
    public static Date getSqlDate(java.util.Date d) {
        //因为sql的Date只有毫秒和年月日创建对象
        //所以使用util的Date里的getTime()获取对应时间毫秒来创建sql的Date
        return new Date(d.getTime());
    }


}
